// pages/cpc/cpc.js
const app = getApp()
var party_notices_list = require('../../data/notice/party.js').notices_list
var js_data = require('../../data/post/post_fixed.js')
var posts_js_data = js_data.cpc_data
Page({
  data: {
    current: 'tab2',
    index: 1,
    tabs: [
      {
          key: 'tab1',
          title: '我的党建',
          content: 'Content of tab 1',
      },
      {
          key: 'tab2',
          title: '党建资讯',
          content: 'Content of tab 2',
      },
      {
          key: 'tab3',
          title: '党员论坛',
          content: 'Content of tab 3',
      },
      {
          key: 'tab4',
          title: '党员活动',
          content: 'Content of tab 4',
      },
    ],
    // 悬浮按钮位置
    movable_x: null,
    movable_y: null,
    banner_notice_list: []
  },

  // 获取屏幕宽高，设置悬浮新建按钮的初始位置
  setMovableViewPosition: function(){
    var windowWidth = wx.getSystemInfoSync().windowWidth
    var windowHeight = wx.getSystemInfoSync().windowHeight
    // console.log(windowHeight)
    var movable_x = String(windowWidth / 2 - 20) + 'px'
    var movable_y = String(windowHeight - 50) + 'px'
    // x="140px" y="450px"
    this.setData({
      movable_x: movable_x,
      movable_y: movable_y
    })
  },

  // 控制tab swiper切换
  onTabsChange(e) {
    // console.log('onTabsChange', e)
    const { key } = e.detail
    const index = this.data.tabs.map((n) => n.key).indexOf(key)

    this.setData({
        key,
        index,
    })
  },
  onSwiperChange(e) {
    // console.log('onSwiperChange', e)
    const { current: index, source } = e.detail
    const { key } = this.data.tabs[index]

    if (!!source) {
        this.setData({
            key,
            index,
        })
    }
  },

  // 获取所有资讯公告的预览信息
  getNoticePreview: function(e) {
    app.sendGetRequest('/community/cpc/notice/preview_with_pic', (res) => {
      // console.log('党建资讯', res)
      var banner_notices = []
      var notices = []
      for(var i=0; i<res.length; i++){
        var row = res[i]
        notices.push(row)
        if(row.topStatus == 1){
          banner_notices.push(row)
        }
      }
      this.setData({
        banner_notice_list: banner_notices,
        notices_list: notices
      })
    }, (err) => {
      console.log(err.errMsg)
    })
  },

  // 获取所有活动的预览信息
  getActivityPreview: function(e) {
    app.sendGetRequest('/community/cpc/activity/list', (res) => {
      // console.log('党建活动', res.rows)
      this.setData({
        activities_list: res.rows
      })
    }, (err) => {
      console.log(err.errMsg)
    })
  },

  // 获取所有论坛帖的预览信息
  getPostPreview: function(e) {
    app.sendGetRequest('/community/cpc/post/list', (res) => {
      // console.log('党员帖子', res.rows)
      this.setData({
        posts_list: res.rows
      })
    }, (err) => {
      console.log(err.errMsg)
    })
  },

  onLoad: function (options) {
    this.getNoticePreview()
    this.getActivityPreview()
    this.getPostPreview()
    // 读取屏幕宽高,动态调整悬浮按钮的位置
    this.setMovableViewPosition()
    var that = this 
    wx.getSystemInfo({ 
        success: function (res) { 
            that.setData({ 
                clientHeight: res.windowHeight-200 
            }); 
        }
    })
  },

  // 点击党建资讯卡片
  onNoticeTap: function(e){
    var id = e.currentTarget.dataset.notice_id
    wx.navigateTo({
      url: './noticeDetail/noticeDetail?id=' + id,
    })
  },

  // 点击活动卡片
  onActivityTap: function(e) {
    var id = e.currentTarget.dataset.activity_id
    wx.navigateTo({
      url: './activityDetail/activityDetail?id=' + id,
    })
  },

  // 点击党员帖子
  onPostTap: function (e){
    var id = e.currentTarget.dataset.post_id
    wx.navigateTo({
      url: './postDetail/postDetail?id=' + id,
    })
  },

  // 创建帖子
  createPost: function (e){
    wx.navigateTo({
      url: './createPost/createPost',
    })
  },

  onMeTap: function (e){
    var type = e.currentTarget.dataset.type
    wx.navigateTo({
      url: './me/me?type='+type,
    })
  },
  
})

